 #include <cv.h>
 #include <cxcore.h>
 #include <highgui.h>  
 #include <iostream> 
 #include "Segmentation.h"
 using namespace std;  
   
 void processFrame(IplImage*& f);
     const char *windowName1 = "First steps with OpenCV1";  
  
 int main(int argc, char** argv)   
 {  
  
  CvCapture* capture = 0;
  capture = cvCreateCameraCapture(0);  
  if (!capture)  
  {  
   cout << "Initialization failed" << endl;  
   return EXIT_FAILURE;  
 }  
 
  const char *windowName = "First steps with OpenCV";  
  cvNamedWindow(windowName, CV_WINDOW_AUTOSIZE);  
  cvNamedWindow(windowName1, CV_WINDOW_AUTOSIZE);
  while(true)   
  {  
 
   IplImage* f = cvQueryFrame( capture );

   if(!f)   
    break; 
        cvShowImage(windowName, f);	
		processFrame(f);
		//cvCvtColor(f,f,CV_HSV2BGR);
   char c = cvWaitKey(33);     
   if (c == 27)

	   break;
  }

  cvReleaseCapture( &capture );  
  cvDestroyWindow(windowName);  
  cvDestroyWindow(windowName1);  
 return 0;  
 } 
  
 
 void processFrame(IplImage*& f)  
    {     
 
 //   CvSize size = cvSize(image->width, image->height);
	//IplImage* newImage = cvCreateImage(size,image->depth,3);
	//newImage = cvCloneImage(image);
	//cvZero(newImage);
	//cvPyrMeanShiftFiltering(image,newImage, 1, 18, 3,
	//	cvTermCriteria(CV_TERMCRIT_EPS, 20, 0.3 )
	//;
	//cvShowImage(windowName1, newImage);  
 //   cvReleaseImage(&newImage);  
		CvSize size = cvSize(f->width, f->height);
		IplImage* fGray = cvCreateImage(size,f->depth,1);		
		cvCvtColor(f,fGray,CV_RGB2GRAY);
		MyImage* frame = new MyImage (fGray);
		frame->UpdatePlanes();
		BarGraph* BarGR = new BarGraph(frame->GetPlane());
		int k = 0;
		int* a = BarGR->GetK(k);
		IplImage* fSegments = Segmentation::K_means(a,k,frame);
		delete[] a;
		MyImage* fSum = new MyImage(f);
		fSum->UpdatePlanes();			
		byte* SegPix = (uchar*)fSegments->imageData;
		for (int i =0; i <3; i++)
		{
			double* Pixels = fSum->GetPlane(i)->GetPlaneAdress();
			int pSize = fSum->GetPlane(i)->GetSize();
			for (int i =0; i< pSize; i++)
			{
				Pixels[i] = (Pixels[i] + SegPix[i])/30;
			}

		}
		fSum->UpdateFromPlanes();
		cvShowImage(windowName1,f);
		delete frame;
		delete fSum;
		cvReleaseImage(&fGray);
		cvReleaseImage(&fSegments);
		delete BarGR;
	//double avr =(image->GetPlane(2)->GetAvr());
	//image->GetPlane(2)->SetToConst(avr);
   } 